Overview of testng.xml

Reading Time: 4 minutes

Now a days TestNG(Test Next Generation) started getting popularity and for sure it will be a big name in data driven testing framework and the heart of this framework is testng.xml file. Basically TestNG is designed for developer or testers who wants to go beyond unit testing.

Why TestNG ?

Although this post is not dedicated to explain TestNG we will be focusing more on testng.xml file but before that one most know why TestNG is getting popularity and an idea about how this framework look like. So here is a list of reasons why it is getting popularity.

  • Can do multi threading testing : You may want to perform the load or performance testing for a particular functionality or for parallel testing as well.
  • Uses java : Since java is one of the most popular language that give’s it an upper hand in its popularity.
  • Maintains flexibility : By providing run time configuration and upholds group and partial testing.
  • Larger coverage area : Means you can perform API as well UI testing.

What is testng.xml?

Lets have quick introduction of testng.xml file. It is configuration file from where you can actually trigger your test methods and helps you to better manage your test cases like you ever imagined, also used to run test cases.

For example suppose you have been asked to test a particular feature on day1 then regression testing on day2 then may be asked to test some feature 2 on day 3 followed by smoke testing on the next day, now this is tedious and not so many people enjoy to run selectively only those test cases according to the requirement.
So, this testng.xml file come into play where it will be only one time effort to group those test cases moreover it is configurable that makes it flexible as well. It is like only one file which can solve so many problems and that’s all you can be lazy know. Simply reducing time for all those repeated task. How exactly it will be done is discussed in later part of the blog.
This is what a sample testng.xml file look like :-

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite>
    <test name = "HealthCheck">
         <classes>
            <class name="RegistrationTest"/>
          </classes>
    </test>
</suite>

Hook into this configuration file

  • This file consist of a suite preferably given a test name which we have given as HealthCheck.
    test suite is a collection of test cases intended to test a behaviour or a set of behaviours of software program. Generally it a part of testing source code but here we are defining test suite in a seprate xml file.
  • One suite can have one or more testng classes and in each class you can have any number of test cases.
  • A testng class is a class which consist of at least one testng annotation and these classes are the entry point for running test cases in testng framework.
  • If you execute this script will run only those test cases which are in TestngAnnotation file and at the end of you will get nice report of executed, passed and failed test cases.
  • Before we finish over-viewing the structure of testng.xml file keep these two points in mind.
    !! We will be keeping this file into the project parallel to the src directory because from there you can have access to test as well as main java classes.
    !! You should be giving full-fledged path of the class i.e. packagename.classname

Defining a testng class

As I said earlier a testng class class consist of atleast one or more testng annotation.
These are few examples :-

  • @test
  • @beforeMethod
  • @afterSuit
  • @afterClass
    For complete list visit the official documentation.

To recognise test cases we use @Test annotation while all other annotations are just supporting methods.
When using testng framework we don’t use any main method to run these test cases as these above mentioned test cases runs it internally by making them static function.
Lets not go very far with testng and keep ourselves near its xml configuration file. Apart from maintaining test cases we can also generate HTML reports and loggers.

Grouping of test cases

There is another very cool and innovative functionality of tesng.xml file called grouping. Suppose in a class you don’t want to run every test cases, instead you are planning to run few major test cases since you want to save your resources or may be some other reason. Even this is also manageable with this new grouping feature apart from this you can make a group of groups and then in next scenario just want to remove a group from your set of groups. This all is managable in our script. Lets have a look.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "API Automation test run">
    <test name = "HEALTH CHECK">
        <groups name="all">
            <run>
                <include name = "smoke"/>
                <exclude name = "regression"/>
            </run>
        </groups>
        <classes>
            <class name="RegistrationTest"/>
            <class name="ListUsersTest"/>
          </classes>
    </test>
</suite>

The script above, we have defined a group that consist of all those test cases which are having smoke tag on it. Now you can clearly see we have excluded test cases with tag regression. Basically it will execute only those test cases in class ListUsersTest and RegistrationTest which are having smoke tag on it. Here we have only one group, we can have multiple test groups and run selective test groups through run tag.

Hope you guys like this post. Let me know what is your opinion and suggestion in comment below. Thank you for be with me. See you in the next part of this blog.

Reference:- https://testng.org/doc/documentation-main.html#testng-xml


Knoldus-blog-footer-image

Written by 

Alok Jha is the QA Consultant at Knoldus Software LLP. He has good knowledge of languages Java, Java 8, Rust and JavaScript. As a QA, he always tries to explore the different type of software and tools.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading